//
//  Team9dairyCell.m
//  Team9Project
//
//  Created by dlios on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9dairyCell.h"

#import <Masonry.h>
#import "JQAFNetworkTool.h"
#import "Team9dairyModel.h"

@implementation Team9dairyCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatAllView];
    }
    return self;
}


- (void)setDairyModel:(Team9dairyModel *)dairyModel{
    if (_dairyModel != dairyModel) {
        _dairyModel = dairyModel;
    }
    
    self.timeLabel.text = self.dairyModel.post_date;
    self.typeLabel.text = self.dairyModel.section_name;
    
    self.describleLabel.text = self.dairyModel.content;
   
    
    // 图片

        for (int i = 0; i < 4; i++) {
            UIImageView *imageView = self.imageViews[i];
            imageView.userInteractionEnabled = NO;
            imageView.image = nil;
            
            if (self.dairyModel.pics.count > i) {
                
                imageView.userInteractionEnabled = YES;
                [Team9Tool sd_imageView:imageView image:self.dairyModel.pics[i][@"pic"] placeholder:[UIImage imageNamed:@"111"]];
            }
        }
        

    
    


    
    
    
    
    
    
    
    
    
}








-(void)creatAllView
{
    
    
    // 背景
    self.backView = [[Team9BaseView alloc] initWithType:ViewTypeWhiteBlack];
    [self.contentView addSubview:self.backView];
    self.backView.layer.cornerRadius = 10 / 375.0 * WIDTH;
    
    // 时间
    self.timeLabel = [[Team9BaseLabel alloc] init];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:14 / 375.0 * WIDTH];
    self.timeLabel.backgroundColor = [UIColor orangeColor];
    self.timeLabel.layer.cornerRadius = 8 / 375.0 * WIDTH;
    self.timeLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:self.timeLabel];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    
    // 类型
    self.typeLabel = [[Team9BaseLabel alloc] init];
    self.typeLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    [self.contentView addSubview:self.typeLabel];
    self.typeLabel.backgroundColor = [UIColor orangeColor];
    self.typeLabel.textColor = [UIColor whiteColor];
    self.typeLabel.layer.cornerRadius = 8 / 375.0 * WIDTH;
    self.typeLabel.layer.masksToBounds = YES;
    self.typeLabel.textAlignment = NSTextAlignmentCenter;
    
    
    // 内容
    self.describleLabel = [[Team9BaseLabel alloc] init];
    self.describleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.describleLabel];
    self.describleLabel.numberOfLines = 0;
    [self.describleLabel sizeToFit];
    self.describleLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    
    
    
    // 图片
    self.imageViews = @[].mutableCopy;
    for (int i = 0; i < 4; i++) {
            UIImageView *imageView = [UIImageView new];
            [self.imageViews addObject:imageView];
        
//          imageView.userInteractionEnabled = YES;
        
        
        // 添加手势
        // 设置点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];

        self.imageViews[i].tag = 1000 + i;

        
            [self.contentView addSubview:imageView];
        
        }

    
  
    
    
    
    
    [self creat_Constraints];
    
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
   
    
    if (_block) {
        self.block(tap.view.tag);
    }
    
    
    
    
}

- (void)creat_Constraints{
    
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(35 / 375.0 * WIDTH);
        make.left.equalTo(self.contentView.mas_left).offset(10 / 375.0 * WIDTH);
        make.right.equalTo(self.contentView.mas_right).offset(-10 / 375.0 * WIDTH);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(5 / 375.0 * WIDTH);
        make.left.equalTo(self.contentView.mas_left).offset(10 / 375.0 * WIDTH);
        make.bottom.equalTo(self.backView.mas_top).offset(-5);
        make.width.mas_equalTo(90 / 375.0 * WIDTH);
        
        
    }];
    
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView.mas_top).offset(10 / 375.0 * WIDTH);
        make.left.equalTo(self.backView.mas_left).offset(10 / 375.0 * WIDTH);
        make.width.mas_equalTo(70 / 375.0 * WIDTH);
        make.height.mas_equalTo(18 / 667.0 * HEGHT);
        
    }];
    
    

    [self.describleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.equalTo(self.typeLabel.mas_bottom).offset(10 / 375.0 * WIDTH);
        make.right.equalTo(self.backView.mas_right).offset(-10 / 375.0 * WIDTH);
    }];
    
    
    UIImageView *lastView = nil;
    for (UIImageView *imageView1 in self.imageViews) {
        [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.describleLabel.mas_bottom).offset(10 / 375.0 * WIDTH);
            if (lastView != nil) {
                make.left.equalTo(lastView.mas_right).offset(1 / 375.0 * WIDTH);
            } else make.left.equalTo(self.backView).offset(10 / 375.0 * WIDTH);
            make.width.height.equalTo(@((WIDTH - 3 / 375.0 * WIDTH - 40 / 375.0 * WIDTH)  / 4));
        }];
        lastView = imageView1;
        
    }

    
    
    
    
    
    
    
}

@end
